Tree   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 15
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A constructor 0 3 1
A BFS 0 8 2
A DFS 0 8 2
1
class Node {
2
    // put your code here to address problems
3
    constructor(data = null) {
4
        this.data = data;
5
        this.children = [];
6
    }
7
8
    /**
9
     * add data as a new child node
10
     * @param data 
11
     */
12
    add(data) {
13
        this.children.push(new Node(data));
14
    }
15
16
    /**
17
     * remove data from children list
18
     * @param data 
19
     */
20
    remove(data) {
21
        this.children = this.children.filter(e => {
22
            return JSON.stringify(e.data) !== JSON.stringify(data)
23
        });
24
    }
25
}
26
27
class Tree {
28
    // put your code here to address problems
29
    constructor() {
30
        this.root = null;
31
    }
32
    /**
33
     * breadth first traveral
34
     * @param fn : a function
35
     */
36
    BFS(fn) {
37
        let queue = [this.root];
38
        while (queue.length) {
39
            let node = queue.shift();
40
            queue.push(...node.children);
41
            fn(node);
42
        }
43
    }
44
45
    /**
46
     * depth first traversal
47
     * @param fn : a function
48
     */
49
    DFS(fn) {
50
        let stack = [this.root];
51
        while (stack.length) {
52
            let node = stack.shift();
53
            stack.unshift(...node.children);
54
            fn(node);
55
        }
56
    }
57
}
58
59
module.exports = {
60
    Node,
61
    Tree
62
};